<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Common;
use App\Http\Controllers\ErrorCode;
use App\Http\Controllers\SuccessCode;
use App\Http\Requests\UserRequest;
use App\Model\GameConfig;
use App\Model\UserCenter;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Http\Request;
use Validator;

class User
{
    use Common;
    public function verify(Request $request)
    {
        $validator = Validator::make($request->all(),[
            'game_id' => 'required|exists:game_config,game_id',
            'token' => 'required',
            'uid' => 'required',
            'username' => 'required'
        ],[
            'game_id.required' => '游戏ID必填',
            'game_id.exists' => '游戏ID不存在',
            'token.required' => 'token 必填',
            'uid.required' => 'uid 必填',
            'username.required' => '用户名必填',
        ]);

        if ($validator->fails()){
            $errors = $validator->errors();
            foreach ( $errors->all() as $message ) {
                return $this->getView([],sprintf(ErrorCode::PARAMS_ERROR,$message));
            }
        }

        $key = GameConfig::where('game_id',$request->input('game_id'))->first();

        $token = md5($request->input('game_id') . $request->input('uid') . $request->input('username') . $key->value);

        if ($token != $request->input('token')){
            return $this->getView([],ErrorCode::TOKEN_ERROR);
        }

        $findRes = UserCenter::where('uid',$request->input('uid'))->first();

        if ($findRes){
            return $this->getView([],SuccessCode::USER_VERIFY_SUCCESS);
        }

        return $this->getView([],ErrorCode::USER_VERIFY_ERROR);
    }
}
